package com.pig4cloud.pig.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.demo.Service.CompanyService;
import com.pig4cloud.pig.demo.Util.TokenUtils;
import com.pig4cloud.pig.demo.feign.Entity.Company;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;


/**
 * @author pig code generator
 * @date 2020-11-03 14:42:09
 */
@Inner
@RestController
@RequiredArgsConstructor
@RequestMapping("/article/company" )
@Api(value = "company", tags = "职位认证管理")
public class CompanyController {

    private final CompanyService companyService;


    /**
     * 分页查询
     * @param page 分页对象
     * @param company
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('generator_company_get')" )
    public R getCompanyPage(Page page, Company company) {
        return R.ok(companyService.page(page, Wrappers.query(company)));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('generator_company_get')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(companyService.getById(id));
    }

    /**
     * 新增
     * @param company
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('generator_company_add')" )
    public R save(@RequestBody Company company) {
        return R.ok(companyService.save(company));
    }

    /**
     * 修改
     * @param company
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @Inner(value = false)
    @PutMapping("/update")
    public R updateById(@RequestBody Company company) {
        return R.ok(companyService.updateById(company));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('generator_company_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(companyService.removeById(id));
    }



    /**
     * 根据用户信息获取已审核的公司信息
     * @return
     */
    @Inner(value = false)
    @GetMapping("/getByUserId")
    public R getByUserId(HttpServletRequest request){
        Long id = TokenUtils.getId(request);
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        //已审核才能查询到
        wrapper.eq("status", 1);

        //数据库已存在该用户的职位信息，则更新数据
        Company company = companyService.getOne(wrapper);
        return R.ok(company);
    }

}
